﻿using Framework;
using RecipeModule;
using UnityEngine;
using UnityEngine.UI;

namespace View
{
    public class CreateRecipeView : ViewProxy
    {
        private ClickTrigger _triggerClose;
        private Toggle[] _toggleRecipeTypes;
        private InputField _inputName;
        private ClickTrigger _triggerConfirm;
        private CreateRecipeGrid[] _recipeIconGrids;
        private int _selectIconIndex = -1;

        private Recipe _recipe;
        
        protected override void SetupView()
        {
            LoadRes("Views/Recipe/CreateRecipeView");
        }

        protected override void OnInit()
        {
            _recipe = RecipeManager.instance.CreateEmptyRecipe();
            
            _triggerClose = ClickTrigger.Get(_transform.Find("Button_Close"));
            _triggerClose.AddClickListener(OnCloseClick);
            _triggerConfirm = ClickTrigger.Get(_transform.Find("Button_Confirm"));
            _triggerConfirm.AddClickListener(OnConfirmClick);

            _inputName = _transform.Find("Input_Name").GetComponent<InputField>();
            
            _toggleRecipeTypes = new Toggle[3];
            for (int i = 0; i < 3; ++i)
            {
                Toggle toggle = _transform.Find("RecipeType/Toggle_" + (i + 1)).GetComponent<Toggle>();
                toggle.onValueChanged.AddListener(OnRecipeTypeChanged);
                _toggleRecipeTypes[i] = toggle;
            }

            GameObject gridPrefab = _transform.Find("Scroll View/Viewport/Content/Grid").gameObject;
            _recipeIconGrids = new CreateRecipeGrid[DishConfig.Dishes.Length];
            for (int i = 0; i < DishConfig.Dishes.Length; ++i)
            {
                Dish dish = DishConfig.Dishes[i];
                CreateRecipeGrid grid = new CreateRecipeGrid(GameObject.Instantiate(gridPrefab), dish.id);
                grid.AddTo(gridPrefab.transform.parent);
                ClickTrigger trigger = ClickTrigger.Get(grid.GetTransform());
                trigger.AddClickListener(OnGridClick, i);
                _recipeIconGrids[i] = grid;
            }
            gridPrefab.SetActive(false);
            
            SetSelectIndex(0);

            Update();
        }

        protected override void OnDispose()
        {
            for (var i = 0; i < _recipeIconGrids.Length; i++)
            {
                _recipeIconGrids[i].Dispose();
            }
            _recipeIconGrids = null;
        }

        private void Update()
        {
            for (var i = 0; i < _toggleRecipeTypes.Length; i++)
            {
                _toggleRecipeTypes[i].isOn = i == (int)_recipe.recipeType;
            }
        }

        private void SetSelectIndex(int selectIndex)
        {
            if (_selectIconIndex != selectIndex)
            {
                if (_selectIconIndex != -1)
                {
                    _recipeIconGrids[_selectIconIndex].SetSelect(false);
                }
                _selectIconIndex = selectIndex;
                _recipeIconGrids[_selectIconIndex].SetSelect(true);
            }
        }

        private void OnGridClick(params object[] args)
        {
            int index = (int) args[0];
            SetSelectIndex(index);
        }

        private void OnRecipeTypeChanged(bool value)
        {
            RecipeType recipeType = _recipe.recipeType;
            for (var i = 0; i < _toggleRecipeTypes.Length; i++)
            {
                if (_toggleRecipeTypes[i].isOn)
                {
                    recipeType = (RecipeType) i;
                    break;
                }
            }
            _recipe.recipeType = recipeType;
            Update();
        }

        private void OnCloseClick(params object[] args)
        {
            Close();
        }

        private void OnConfirmClick(params object[] args)
        {
            _recipe.name = _inputName.text;
            _recipe.iconId = _recipeIconGrids[_selectIconIndex].GetIconId();
            if (!string.IsNullOrEmpty(_recipe.name))
            {
                RecipeManager.instance.AddRecipe(_recipe);
                RecipeInfoView recipeInfoView = new RecipeInfoView();
                recipeInfoView.SetRecipeId(_recipe.id);
                recipeInfoView.Show();
                Close();
            }
        }

        private class CreateRecipeGrid
        {
            private GameObject _gameObject;
            private RectTransform _transform;

            private SquareDishIconProxy _iconProxy;
            private GameObject _goMark;

            public CreateRecipeGrid(GameObject gameObject, int iconId)
            {
                _gameObject = gameObject;
                _transform = (RectTransform) _gameObject.transform;
                _iconProxy = new SquareDishIconProxy();
                _iconProxy.AddTo(_transform.Find("Icon"));
                _goMark = _transform.Find("Image_Mark").gameObject;
                _iconProxy.SetIconId(iconId);
                SetSelect(false);
            }

            public void Dispose()
            {
                _iconProxy.Dispose();
                GameObject.Destroy(_gameObject);
            }
            
            public RectTransform GetTransform()
            {
                return _transform;
            }

            public int GetIconId()
            {
                return _iconProxy.GetIconId();
            }

            public void SetSelect(bool value)
            {
                _goMark.SetActive(value);
            }

            public void AddTo(Transform parent)
            {
                _transform.SetParent(parent, false);
            }
        }
    }
}